// DAY
var day = new Date().getDate();

// MONTH
var numberOfMonth = new Date();
var monthName = new Array(11);
monthName[0]="January"; monthName[1]="February"; monthName[2]="March"; monthName[3]="April"; monthName[4]="May"; monthName[5]="June";
monthName[6]="July"; monthName[7]="August"; monthName[8]="September"; monthName[9]="October"; monthName[10]="November"; monthName[11]="December";
var month = monthName[numberOfMonth.getMonth()];

// YEAR
var year = new Date().getFullYear();

// HOUR
var hours = new Date().getHours();
var am_pm = 'AM';
	if(hours < 12){ am_pm = 'AM'; }else{ am_pm = 'PM'; }
	if(hours > 12){ hours = hours - 12; }
var minutes = new Date().getMinutes();

// DAY OF WEEK
var numberOfday = new Date();
var weekDay = new Array(7);
weekDay[0]="Sunday"; weekDay[1]="Monday"; weekDay[2]="Tuesday"; weekDay[3]="Wednesday"; weekDay[4]="Thursday"; 
weekDay[5]="Friday"; weekDay[6]="Saturday";
var dayOfWeek = weekDay[numberOfday.getDay()];

// MONTH DAYS
var monthDays = new Date(0).getDate();

// BEGIN DAY
var numberOfBDay = new Date();
numberOfBDay.setFullYear(year);
var thisMonth = new Date().getMonth(); 
numberOfBDay.setMonth(thisMonth);
numberOfBDay.setDate(1);
var beginDay = weekDay[numberOfBDay.getDay()];

// CHANGE VALUES DEPENDING OF REQUIREMENT
var requestMonth = numberOfMonth.getMonth();
// Previous Month
function prevMonth() {
	// month
	if(requestMonth > 0){ requestMonth = requestMonth - 1; }else{ requestMonth = 11; }
	month = monthName[requestMonth];
	// year
	if(requestMonth == 11){ year = year - 1; }
	// month days
	rMonthDays = new Date(year, requestMonth + 1, 0).getDate();
	monthDays = rMonthDays;
	// begin day
	numberOfBDay.setFullYear(year);
	numberOfBDay.setMonth(requestMonth);
	numberOfBDay.setDate(1);
	beginDay = weekDay[numberOfBDay.getDay()];
	//printCalendar
	printCalendar();
}
// Next Month
function nextMonth() {
	// month
	if(requestMonth < 11){ requestMonth = requestMonth + 1; }else{ requestMonth = 0; }
	month = monthName[requestMonth];
	// year
	if(requestMonth == 0){ year = year + 1; }
	// month days
	rMonthDays = new Date(year, requestMonth + 1, 0).getDate();
	monthDays = rMonthDays;
	// begin day
	numberOfBDay.setFullYear(year);
	numberOfBDay.setMonth(requestMonth);
	numberOfBDay.setDate(1);
	beginDay = weekDay[numberOfBDay.getDay()];
	//printCalendar
	printCalendar();
}
// PRINT CALENDAR
function printCalendar(){
	$("#calendar").empty('');
	// PRINT ALL TE INFO
	var x = "Today is <b>"+dayOfWeek+" "+day+"</b> of <b>"+month+"</b>";
	x+=" of <b>"+year+"</b> at <b>"+(hours<10?'0':'')+hours+":"+(minutes<10?'0':'')+minutes+" "+am_pm+"</b>";
	x+=" and this month has <b>"+monthDays+"</b> days beginning on <b>"+beginDay+"</b>";
	$("#txt").html(x);
	
	// CREATE THE CALENDAR
	var calendarDays = "<tr>";
	var cuentaTd = 0;
	// INSERT DAYS OF WEEK
	if(beginDay == "Monday"){ calendarDays+="<td></td>"; cuentaTd = 1; }
	else if(beginDay == "Tuesday"){ calendarDays+="<td></td><td></td>"; cuentaTd = 2; }
	else if(beginDay == "Wednesday"){ calendarDays+="<td></td><td></td><td></td>"; cuentaTd = 3; }
	else if(beginDay == "Thursday"){ calendarDays+="<td></td><td></td><td></td><td></td>"; cuentaTd = 4; }
	else if(beginDay == "Friday"){ calendarDays+="<td></td><td></td><td></td><td></td><td></td>"; cuentaTd = 5; }
	else if(beginDay == "Saturday"){ calendarDays+="<td></td><td></td><td></td><td></td><td></td><td></td>"; cuentaTd = 6; }
	// INSERT DAYS NUMBERS
	for(n=1; n<=monthDays; n++){
	  if( cuentaTd == 0){ calendarDays+="<tr>"; }
	  calendarDays+="<td onclick=\"setValue("+(n<10?'0':'')+n+")\">"+n+"</td>";
	  if( cuentaTd == 6){ calendarDays+="</tr>"; }
	  if( cuentaTd != 6){ cuentaTd = cuentaTd+1; }else{ cuentaTd = 0; }
	}
	// INSERT ENDS TDs
	if( cuentaTd == 1){ calendarDays+="<td><td><td><td><td><td><td><td><td><td></tr>"; }
	else if( cuentaTd == 2){ calendarDays+="<td><td><td><td><td><td><td><td></tr>"; }
	else if( cuentaTd == 3){ calendarDays+="<td><td><td><td><td><td></tr>"; }
	else if( cuentaTd == 4){ calendarDays+="<td><td><td><td></tr>"; }
	else if( cuentaTd == 5){ calendarDays+="<td><td></tr>"; }
	else if( cuentaTd == 6){ calendarDays+="</tr>"; }
	// PRINT THE CALENDAR
	var calendar = '<table cellspacing="0" cellpadding="0" border="0" class="calendar">';
	calendar+='<thead><tr><td onclick="prevMonth()"><</td><td colspan="5">'+month+" "+year+'</td><td onclick="nextMonth()">></td></tr>';
	calendar+='<tr><td class="tdCalendar">Sun</td><td class="tdCalendar">Mon</td><td class="tdCalendar">Tue</td>'
	calendar+='<td class="tdCalendar">Wed</td><td class="tdCalendar">Thu</td><td class="tdCalendar">Fri</td><td class="tdCalendar">Sat</td></tr></thead>';
	calendar+=calendarDays;
	calendar+='</table>';
	$("#calendar").html(calendar);
}
// PRINT THE CALENDAR AND INFO
$(document).ready(function(){
	printCalendar();
});

// SET VARIABLES FOR MOUSE POSITION
var posX = 0;
var poxY = 0;
// SET VARIABLES FOR ACTIVE INPUTS, TYPE OF DATE AND DISPLAY ORDER
var activeInput = '';
var typeOfDate = 1;
var displayOrder = 'dd/mm/yyyy';
// GET MOUSE POSITION
$(document).mousemove(function(e){
	posX = e.pageX;
    posY = e.pageY;
});
// SHOW CALENDAR
function showCalendar(tag, typeDate, displayDate) {
	$("#fdDivVolatil").fadeIn();
	$("#calendar").css({ 'display': 'block' });
	activeInput = tag;
	typeOfDate = typeDate;
	if(displayDate != ''){
		displayOrder = displayDate;
	}else{
		alert('You need to specify the display order of date (dd/mm/yyyy)');
	}
}
// SET VALUE TO ACTIVE INPUT
function setValue(date) {
	if(displayOrder == 'dd/mm/yyyy'){
		if(typeOfDate == 1){
			var numberMonthTemp = requestMonth+1;
			var finalValue = (date<10?'0':'')+date+"/"+(numberMonthTemp<10?'0':'')+numberMonthTemp+"/"+year+" "+(hours<10?'0':'')+hours+":"+(minutes<10?'0':'')+minutes+" "+am_pm;
		}else if(typeOfDate == 2){
			var numberMonthTemp = requestMonth+1;
			var finalValue = (date<10?'0':'')+date+"/"+(numberMonthTemp<10?'0':'')+numberMonthTemp+"/"+year;
		}else{
			alert('Remember that you can only use the number "1" for a date with hour (00/00/0000 00:00 AA) and "2" for a simple date (00/00/0000)');
		}
	}else if(displayOrder == 'mm/dd/yyyy'){
		if(typeOfDate == 1){
			var numberMonthTemp = requestMonth+1;
			var finalValue = (numberMonthTemp<10?'0':'')+numberMonthTemp+"/"+(date<10?'0':'')+date+"/"+year+" "+(hours<10?'0':'')+hours+":"+(minutes<10?'0':'')+minutes+" "+am_pm;
		}else if(typeOfDate == 2){
			var numberMonthTemp = requestMonth+1;
			var finalValue = (numberMonthTemp<10?'0':'')+numberMonthTemp+"/"+(date<10?'0':'')+date+"/"+year;
		}else{
			alert('Remember that you can only use the number "1" for a date with hour (00/00/0000 00:00 AA) and "2" for a simple date (00/00/0000)');
		}
	}else if(displayOrder == 'dd/mmm/yyyy'){
		if(typeOfDate == 1){
			var finalValue = (date<10?'0':'')+date+"/"+monthName[requestMonth]+"/"+year+" "+(hours<10?'0':'')+hours+":"+(minutes<10?'0':'')+minutes+" "+am_pm;
		}else if(typeOfDate == 2){
			var finalValue = (date<10?'0':'')+date+"/"+monthName[requestMonth]+"/"+year;
		}else{
			alert('Remember that you can only use the number "1" for a date with hour (00/00/0000 00:00 AA) and "2" for a simple date (00/00/0000)');
		}
	}else if(displayOrder == 'mmm/dd/yyyy'){
		if(typeOfDate == 1){
			var finalValue = monthName[requestMonth]+"/"+(date<10?'0':'')+date+"/"+year+" "+(hours<10?'0':'')+hours+":"+(minutes<10?'0':'')+minutes+" "+am_pm;
		}else if(typeOfDate == 2){
			var finalValue = monthName[requestMonth]+"/"+(date<10?'0':'')+date+"/"+year;
		}else{
			alert('Remember that you can only use the number "1" for a date with hour (00/00/0000 00:00 AA) and "2" for a simple date (00/00/0000)');
		}
	}else{
		alert("There is a trouble with your variables,  use one of this examples - dd/mm/yyyy , mm/dd/yyyy, dd/mmm/yyyy, mmm/dd/yyyy");
	}
	$("."+activeInput).val(finalValue);
	$("#calendar").hide();
	$("#fdDivVolatil").hide();
}